package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 地址簿管理
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
@Api(tags = "地址簿管理")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderService orderService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        //设置用户id
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);
        log.info("新增收获地址：{}", addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询指定用户的全部id
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        // log.info("全部用户：{}",addressBook);
        //查询当前用户登录的id
        Long userId = BaseContext.getCurrentId();
        //addressBook.setUserId(userId);

        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AddressBook::getUserId, userId);
        List<AddressBook> addressBooks = addressBookService.list(wrapper);
        return R.success(addressBooks);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> savedefault(@RequestBody AddressBook addressBook) {
        log.info("默认地址参数：{}", addressBook);
        //将所有的地址参数设置为0

        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, userId);
        wrapper.set(AddressBook::getIsDefault, 0);

        addressBookService.update(wrapper);

        //在将需要修改的默认地址修改为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询地址
     *
     * @return
     */
    @GetMapping("/id")
    public R get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<AddressBook> getDefault() {

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook == null) {
            return R.error("查询对象不存在");
        }

        return R.success(addressBook);
    }

    /**
     * 根据id进行地址的回显
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<AddressBook> getAddress(@PathVariable Long id) {
        //进行数据的回显
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, AddressBook::getId, id);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        return R.success(addressBook);
    }

    /**
     * 修改收货地址
     *
     * @return
     */
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("地址修改成功");
    }

    /**
     * 删除地址
     *
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids.size() >= 1, Orders::getAddressBookId, ids).in(Orders::getStatus, 1, 2, 3);
        List<Orders> list = orderService.list(queryWrapper);
        if (list.size() >= 1) {
            return R.error("有订单尚未完成，地址不能删除");
        }

        addressBookService.removeByIds(ids);
        return R.success("地址已删除");
    }
}
